﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using RuleCode.Common;


namespace chatblog.usercate
{
    public partial class userCate : System.Web.UI.Page
    {
        private static string currentUser = "";

        #region 页面Load事件
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(chatblog.usercate.userCate));
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                currentUser = AliApi.appUserId;
                BindUserCate();
            }
        }
        #endregion

        #region 绑定分组右键菜单
        public void BindUserCate()
        {
            this.DoorP.InnerHtml = "";
            string childCate = string.Empty;
            string childUser = string.Empty;
            string groupListFilter = string .Empty ;

            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUser + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    
                    groupListFilter = "cbusergroupid='" + userGroup.cbusergroupid.Value + "'";
                    cbusergrouplist groupList = cbusergrouplist.Find(groupListFilter);
                    
                    int count = groupList.Count;
                    childCate = "<h2  class=\"h2style\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "<a>(" + count.ToString() + ")</a></h2>";
                    
                    childCate += "<div class=\"content \">";
                    if (groupList.Count > 0)
                    {
                        do
                        {
                            childCate += "<span class=\"contextMenu0\"  id='"+groupList .cbusergroupid .Value +"' >" + groupList.cbuserid.Value + "</span>" + "<br />";
                        } while (groupList.Next());
                    }
                    childCate += "</div>";
                    this.DoorP.InnerHtml += childCate;
                } while (userGroup.Next());
            }
        }
        #endregion

        #region Ajax方法
        /// <summary>
        /// 删除组内用户
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="groupid"></param>
        [AjaxPro.AjaxMethod ]
        public void DeleteUser(string userid,string groupid)
        {
            string sql = "delete cbusergrouplist where cbuserid='" + userid + "' and cbusergroupid='" + groupid + "'";
            SqlHelper.Instance.ExecuteNonQuery(System.Data.CommandType.Text, sql);
        }

        /// <summary>
        /// 获取右键菜单 HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetDoorPInnerHtml()
        {
            string html = string.Empty;
            string childCate = string.Empty;
            string childUser = string.Empty;
            string groupListFilter = string.Empty;

            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUser + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    groupListFilter = "cbusergroupid='" + userGroup.cbusergroupid.Value + "'";
                    cbusergrouplist groupList = cbusergrouplist.Find(groupListFilter);
                    int count = groupList.Count ;
                    childCate = "<h2  class=\"h2style\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "<a>(" + count.ToString() + ")</a></h2>";
                    
                    childCate += "<div class=\"content \">";
                    if (groupList.Count > 0)
                    {
                        do
                        {
                            childCate += "<span class=\"contextMenu0\"  id='" + groupList.cbusergroupid.Value + "' >" + groupList.cbuserid.Value + "</span>" + "<br />";
                        } while (groupList.Next());
                    }
                    childCate += "</div>";
                    html += childCate;
                } while (userGroup.Next());
            }

            return html;
        }

        /// <summary>
        /// 获取 分组列表HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod ]
        public string GetGroupListUlHtml(string userid,string oldGroupid)
        {
            string ulHtml = string.Empty;
            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUser + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    ulHtml += "<li id=\"" + userGroup.cbusergroupid.Value + "\" onclick=\"changeGroupId('" + userid + "','" + oldGroupid + "','" + userGroup.cbusergroupid.Value + "')\" >" + userGroup.cbname.Value + "</li>";  //onclick=\"changeGroupId('dddd')\"
                } while (userGroup.Next());
            }
            return ulHtml;
        }

        /// <summary>
        /// 获取 分组列表HTML代码
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public string GetGroupListSelectHtml(string defaultGroupid)
        {
            string ulHtml = "选择好友分 类：<select style =\" width:100px\" id=\"GroupListSelect\">";
            RuleCode.Table.cbusergroup userGroup = cbusergroup.Find("cbuserid='" + currentUser + "'", " cbusergroupid ");
            if (userGroup.Count > 0)
            {
                do
                {
                    if (defaultGroupid == userGroup.cbusergroupid.Value)
                    {
                        ulHtml += "<option selected=\"true\" id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                    else
                    {
                        ulHtml += "<option id=\"" + userGroup.cbusergroupid.Value + "\">" + userGroup.cbname.Value + "</option>";
                    }
                } while (userGroup.Next());
            }
            ulHtml += "</select>";
            return ulHtml;
        }

        /// <summary>
        /// 更新用户分组信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="oldGroupId"></param>
        /// <param name="newGroupId"></param>
        [AjaxPro.AjaxMethod]
        public void UpdateUserGroup(string userid,string oldGroupId,string newGroupId)
        {
            string sql = "update cbusergrouplist set cbusergroupid='"+newGroupId +"'  where cbuserid='" + userid + "' and cbusergroupid='" + oldGroupId + "'";
            SqlHelper.Instance.ExecuteNonQuery(System.Data.CommandType.Text, sql);
        }

        [AjaxPro .AjaxMethod ]
        public void AddGroup(string cbName)
        {
            cbusergroup group = new cbusergroup();
            group.Init(0);
            group.New();
            group.cbuserid.Value = currentUser;
            group.cbusergroupid.Value = group.GetRecID().ToString();
            group.cbname.Value = cbName;
            group.Insert();
        }

        [AjaxPro.AjaxMethod]
        public void DeleteGroup(string groupid, string cbName)
        {
            string sql = "delete cbusergroup where cbusergroupid='" + groupid + "' and cbname='" + cbName + "'";
            SqlHelper.Instance.ExecuteNonQuery(System.Data.CommandType.Text, sql);
        }

        [AjaxPro.AjaxMethod]
        public bool UpdateGroup(string groupid, string oldName, string newName)
        {
            string sql = "update cbusergroup set cbname='" + newName + "'  where cbusergroupid='" + groupid + "' and cbname='" + oldName + "'";
            return  SqlHelper.Instance.ExecuteNonQuery(System.Data.CommandType.Text, sql)>=1;
        }

        [AjaxPro.AjaxMethod]
        public bool AddGroupUser(string groupid, string userid, string username, string status)
        {
            try
            {
                cbusergrouplist grouplist = new cbusergrouplist();
                grouplist.Init(0);
                grouplist.New();
                grouplist.cbname.Value = username;
                grouplist.cbstatus.Value = status;
                grouplist.cbusergroupid.Value = groupid;
                grouplist.cbuserid.Value = userid;
                grouplist.Insert();
            }
            catch
            {
                return false;
            }
            return true;
        }

        [AjaxPro .AjaxMethod ]
        public int GetGroupUserList(string groupid)
        {
            string filter = "cbusergroupid='"+groupid +"' ";
            cbusergrouplist grouplist = cbusergrouplist.Find(filter );
            return grouplist.Count;
        }
        #endregion

    }
}
